package cn.chencq.cmd.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class CommandUtil {

	public static CommandResult exec(String cmd) {
		int result = -1;
		if (cmd == null || cmd.length() == 0) {
			return new CommandResult(-1, null, null);
		}

		Process process = null;
		StringBuilder success = new StringBuilder();
		StringBuilder error = new StringBuilder();

		BufferedReader sr, er;

		try {
			List<String> cmds = new ArrayList<String>();
			cmds.add("cmd.exe");
			cmds.add("/C");
			cmds.add(cmd);

			String[] commands = new String[cmds.size()];
			cmds.toArray(commands);

			process = Runtime.getRuntime().exec(commands);
			if (process == null) {
				return new CommandResult(-1, null, null);
			}

			// 获取执行cmd命令后的信息
			sr = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
			er = new BufferedReader(new InputStreamReader(process.getErrorStream(), "GBK"));

			String ss = null;
			while ((ss = sr.readLine()) != null) {
				success.append(ss).append("\n");
			}

			String ee = null;
			while ((ee = er.readLine()) != null) {
				error.append(ss);
			}

			result = process.waitFor();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return new CommandResult(result, success.toString(), error.toString());
	}

	public static CommandResult exec(String[] commands, boolean isNeedResultMsg) {
		int result = -1;
		if (commands == null || commands.length == 0) {
			return new CommandResult(result, null, null);
		}
		Process process = null;
		BufferedReader successResult = null;
		BufferedReader errorResult = null;
		StringBuilder successMsg = null;
		StringBuilder errorMsg = null;
		DataOutputStream os = null;
		try {
			process = Runtime.getRuntime().exec("cmd.exe /c");
			os = new DataOutputStream(process.getOutputStream());
			for (String command : commands) {
				if (command == null)
					continue;
				os.write(command.getBytes());
				os.writeBytes("\n");
				os.flush();
			}
			os.writeBytes("exit\n");
			os.flush();
			result = process.waitFor();
			if (isNeedResultMsg) {
				successMsg = new StringBuilder();
				errorMsg = new StringBuilder();
				successResult = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
				errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"));
				String s;
				while ((s = successResult.readLine()) != null) {
					successMsg.append(s);
				}
				while ((s = errorResult.readLine()) != null) {
					errorMsg.append(s);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				if (successResult != null) {
					successResult.close();
				}
				if (errorResult != null) {
					errorResult.close();
				}
				if (process != null) {
					process.destroy();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}

		}
		return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null : errorMsg.toString());
	}
}
